package com.itheima.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.mapper.EmpExprMapper;
import com.itheima.mapper.EmpMapper;
import com.itheima.pojo.*;
import com.itheima.service.EmpService;
import com.itheima.utils.JwtUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.time.LocalDateTime;
import java.util.*;

@Service
public class EmpServiceImpl implements EmpService {
    @Autowired
    private EmpMapper empMapper;
    @Autowired
    private EmpExprMapper empExprMapper;

    /*@Override
    public PageBean page1(Integer page, Integer pageSize) {
        //1.调用mapper接口获取总记录数 total
        Long total = empMapper.count();

        //2.调用mapper接口 获取结果列表 rows
        //计算起始索引
        Integer start = (page - 1) * pageSize;
        List<Emp> rows = empMapper.list(start , pageSize);
        *//* 3.封装结果 *//*
        return new PageBean(total,rows);
    }*/

    @Override
    public PageBean page(EmpQueryParam queryParam) {
        //1. 设置分页参数
        PageHelper.startPage(queryParam.getPage(), queryParam.getPageSize());

        //2. 执行查询
        List<Emp> empList = empMapper.list(queryParam);

        //3. 解析封装分页结果
        Page<Emp> p = (Page<Emp>) empList;

        return new PageBean(p.getTotal(), p.getResult());
    }
    @Transactional(rollbackFor = {Exception.class})//事务管理的注解
    @Override
    public void add(Emp emp) {
        //1.调用empMapper保存员工的基本信息
        emp.setCreateTime(LocalDateTime.now());
        emp.setUpdateTime(LocalDateTime.now());

        // int i =1/0;

        empMapper.insert(emp);
        //2.调用empExprMapper
        List<EmpExpr> exprList = emp.getExprList();
        if (!CollectionUtils.isEmpty(exprList)){
            exprList.forEach(empExpr -> {
                empExpr.setEmpId(emp.getId());
            });
        }
        empExprMapper.insertBatch(exprList);
    }
    @Transactional
    @Override
    public void delete(List<Integer> ids) {
        //1.批量删除员工的基本信息
        empMapper.deleteByIds(ids);
        //2.批量删除员工工作经历
        empExprMapper.deleteByEmpIds(ids);
    }
    @Transactional
    @Override
    public void update(Emp emp) {
        //1.先处理emp表的基础信息的修改
        //更新修改的时间
        emp.setUpdateTime(LocalDateTime.now());
        empMapper.update(emp);
        //2.再处理empexpr工作经历的删除
        empExprMapper.deleteByEmpIds(Arrays.asList(emp.getId()));
        //3.再添加empexpr工作经历的添加
        List<EmpExpr> exprList = emp.getExprList();
        //先判断工作经历是否为空，要是空的话就不用再执行添加工作经历
        if (!CollectionUtils.isEmpty(exprList)){
            exprList.forEach(empExpr -> {
                empExpr.setEmpId(emp.getId());
            });
        }
        empExprMapper.insertBatch(exprList);

    }

    @Override
    public List<Emp> selectAll() {
        return empMapper.selectAll();
    }

    /**
     * 验证登录用户名和密码
     * @param emp
     * @return
     */
    @Override
    public LoginInfo login(Emp emp) {
        Emp e = empMapper.getUserAndPassword(emp);
        //
        if (e != null){//登陆成功 - 生成JWT令牌
            Map<String,Object> claims = new HashMap<>();
            claims.put("id",e.getId());
            claims.put("username",e.getUsername());
            claims.put("name",e.getName());

            String jwt = JwtUtils.generateJwt(claims);

            return new LoginInfo(e.getId(),e.getUsername(),e.getPassword(),jwt);
        }
        return null;
    }

    @Override
    public Emp getInfo(Integer id) {
        return empMapper.getById(id);
    }


}
